//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================
// Code file original author: Martin Lapierre, http://devinstinct.com
//===============================================================================

using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using EntLibContrib.Validation.Validators;

namespace EntLibContrib.Validation.Configuration
{
	/// <summary>
	/// Composite RuleSet Validator Data
	/// </summary>
	public class CompositeRuleSetValidatorData : ValidatorData
	{
		#region Constants
		private const string TargetRuleSetsPropertyName = "targetRuleSets";
		private const string SourcePropertyName = "source";
		private const string ConfigurationSourceNamePropertyName = "sourceConfigurationName";
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the target ruleSets.
		/// </summary>
		/// <value>The target ruleSets.</value>
		[ConfigurationProperty(TargetRuleSetsPropertyName)]
		public string TargetRuleSets
		{
			get { return (string)this[TargetRuleSetsPropertyName]; }
			set { this[TargetRuleSetsPropertyName] = value; }
		}

		/// <summary>
		/// Gets or sets the source.
		/// </summary>
		/// <value>The source.</value>
		[ConfigurationProperty(SourcePropertyName, DefaultValue = ValidationSpecificationSource.Both)]
		public ValidationSpecificationSource Source
		{
			get { return (ValidationSpecificationSource)this[SourcePropertyName]; }
			set { this[SourcePropertyName] = value; }
		}

		/// <summary>
		/// Gets or sets the name of the configuration source.
		/// </summary>
		/// <value>The name of the configuration source.</value>
		[ConfigurationProperty(ConfigurationSourceNamePropertyName)]
		public string ConfigurationSourceName
		{
			get { return (string)this[ConfigurationSourceNamePropertyName]; }
			set { this[ConfigurationSourceNamePropertyName] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="CompositeRuleSetValidatorData"/> class.
		/// </summary>
		public CompositeRuleSetValidatorData()
		{ }

		/// <summary>
		/// Initializes a new instance of the <see cref="CompositeRuleSetValidatorData"/> class.
		/// </summary>
		/// <param name="name">The name of the element.</param>
		public CompositeRuleSetValidatorData(string name)
			: base(name, typeof(CompositeRuleSetValidator))
		{ }
		#endregion

		#region Protected Methods
		/// <summary>
		/// Creates the <see cref="CollectionCountValidator"/> described by the configuration object.
		/// </summary>
		/// <param name="targetType">The type of object that will be validated by the validator.</param>
		/// <returns>
		/// The created <see cref="CollectionCountValidator"/>.
		/// </returns>
		/// <remarks>This operation must be overriden by subclasses.</remarks>
		protected override Validator DoCreateValidator(Type targetType)
		{
			return new CompositeRuleSetValidator(
				targetType,
				CompositeRuleSetValidator.GetTargetRuleSets(TargetRuleSets),
				Source,
				ConfigurationSourceName);
		}
		#endregion
	}
}